/**
 * 
 */
package com.runningfox.humor;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.runningfox.model.Joke;

/**
 * @author lhy
 * 
 */
public class JokeListAdapter extends BaseAdapter {
	ArrayList<Joke> list;
	Context c;
	Humor callback;
	public static String page ;
//	int selectedIndex =-1;
	
	public Joke getPreNode(Joke node){
		int size =list.size();
		 Joke preNode=list.get(0);
		for(int i=0;i<size;i++){
			Joke curnode = list.get(i);
			if(curnode.id==node.id){
				if(i==0)
					return null;
				else
					return preNode;
			}
			preNode= curnode;
		}
		return null;
	}
	
	public Joke getNextNode(Joke node){
		int size =list.size();
		for(int i=0;i<size;i++){
			Joke curnode = list.get(i);
			if(curnode.id==node.id){
				if(i+1<list.size()){
					return list.get(i+1);
				}else{
					return null;
				}
			}
		}		
		return null;
	}
	
	public void setSelected(int index){
//		if(selectedIndex!=-1){
//			list.get(selectedIndex).isSelected=false;
//		}
		if(index>=0){
			int size =list.size();
			for(int i=0;i<size;i++){
				Log.v("loop",  this.getClass().getName()+" setSelected");
				list.get(i).isSelected=false;
			}
			list.get(index).isSelected=true;
			this.notifyDataSetInvalidated();
//			this.notifyDataSetChanged();
//			selectedIndex=index;
		}
	}
	
	/**
	 * 用于分页获取更多内容
	 * @return
	 */
	public int getMinHid(){
		if(list!=null&&this.list.size()>0){
			int size = this.list.size();
			int rtn = list.get(0).id;
			for(int i=0;i<size;i++){
				Log.v("loop",  this.getClass().getName()+" getMinHid");
				int tmp = list.get(i).id;
				if(tmp<rtn){
					rtn=tmp;
				}
			}
			return rtn;
		}
		return -1;
	}
	
	
	public JokeListAdapter(Humor humor) {
		super();
		this.callback = humor;
		this.c = this.callback.c;
	}

	public void setList(ArrayList<Joke> list) {
//		this.selectedIndex=-1;
		this.list = list;
		this.notifyDataSetChanged();
	}

	public void append(Joke next){
		list.add(next);
		this.notifyDataSetChanged();
	}
	
	public void appendList(ArrayList<Joke> list){
		if(this.list==null){
			this.list=list;
		}else{
			int size = list.size();
			for(int i=0;i<size;i++){
				Log.v("loop",  this.getClass().getName()+" appendlist");
				this.list.add(list.get(i));
			}
		}
		this.notifyDataSetChanged();
	}
	public void updateNode(Object node){
		int size = list.size();
		for(int i=0;i<size;i++){
			Object cur = list.get(i);
			if(cur!=null&& cur instanceof Joke&&node instanceof Joke){
				Joke curjoke =(Joke)cur;
				Joke joke = (Joke)node;
				if(curjoke.id==joke.id){
					this.list.remove(i);
					this.list.add(i, joke);
					return;
				}
			}
		}
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list!=null?list.size():0;
//		return this.list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		 return list!=null&&arg0>=0&&arg0<list.size()?list.get(arg0):null;
//		return list.get(arg0);
	}

	public Joke getLastItem(){
		int index = list.size()-1;
		if(index>=0){
			return list.get(index);
		}else{
			
			return null;
		}
	}
	
	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return list!=null&&arg0>=0&&arg0<list.size()?list.get(arg0).id:-1;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		JokeNodeView view =null;
		Joke node = (Joke)list.get(position);
		if(convertView==null){
			view = new JokeNodeView(callback, node);
		}else{
			view = (JokeNodeView)convertView;			
			view.setNode(node);
		}
//		view.setExpended(node.isSelected);
		view.setOnline(callback.isOnline);
		return view;
	}

}
